﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxTreeList;
using DevExpress.XtraPrinting;
using OT.Module.Model;
using OT.Module.BLL;
using OT.BaseLibrary;
using OT.Module.Tree;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using DevExpress.Web.Data;
using System.ComponentModel;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using OT.Common;
using OT.Dev;

public partial class Common_HugeDataPager : DevPage
{
    /***
     *  Page
     */

    #region Page_Init

    override protected void Page_Init(object sender, EventArgs e)
    {
        base.Page_Init(sender, e);
        dataSource.Session = XpoSession;
    }

    #endregion

    #region Page_Load

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && !IsCallback)
        {
            // 其它控件初始化
            InitControlValue();
            // 风格设置
            InitGridStyle(InfoGrid, this.GetType().FullName);
            InitGridStyle(CusorGrid, this.GetType().FullName);

            InfoGrid.DataSource = GetXPCollectionSource(Convert.ToInt32(spinDivValue.Number));
            InfoGrid.DataBind();
            
            dataSource.Criteria = "[DivValue] <= ?";
            dataSource.CriteriaParameters.Add("DivValue", TypeCode.Int32, Convert.ToInt32(spinDivValue.Number).ToString());
        }
        // 调整分页风格
        GridPagerStyleSetting(InfoGrid, PagerStyle.Pager);
        GridPagerStyleSetting(CusorGrid, PagerStyle.Pager);

        GridControl();
    }

    #endregion

    #region InitControlValue

    protected void InitControlValue()
    {
        // 下面加入页面自定义控件初始化代码
    }

    #endregion

    #region GridControl

    private void GridControl()
    {
        InfoGrid.SettingsPager.PageSize = 5;
        InfoGrid.Settings.ShowStatusBar = GridViewStatusBarMode.Visible;

        CusorGrid.SettingsPager.PageSize = 5;
        CusorGrid.Settings.ShowStatusBar = GridViewStatusBarMode.Visible;
    }

    #endregion

    /***
     *  Panel & Grid
     */

    #region ajaxPanel_Callback

    protected void ajaxPanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        // 切换树节点时，要将列表之前选定的行取消
        InfoGrid.Selection.UnselectAll();
        // 数据源使用后期参数绑定，所以要调用DataBind
        InfoGrid.DataBind();
    }

    #endregion

    #region InfoGrid_PreRender

    protected void InfoGrid_PreRender(object sender, EventArgs e)
    {
        // 对列表的每一列进行自动的设置，包括
        // 1、根据字段长度自动调整列宽
        // 2、设置编辑页面的文本不换行
        AutoSettingGridColumnStyle(InfoGrid);
    }

    #endregion

    #region InfoGrid_CustomCallback

    protected void InfoGrid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        // 统一处理新增，批量删除，改变分页风格，打开/关闭过滤行事件
        if (HandleDefaultEvent(e.Parameters, InfoGrid, new Product_BLL(XpoSession)))
            return;

        // 不能统一处理的事件，由页面自行处理

        /**
         * 这里没有使用ObjectDataSource，而是直接绑定，
         * 是为了在状态行中准确提示查询耗时与内存用量
         * 不应该在其他页面中使用这种调用方法
         */
        if (e.Parameters.Equals(GEvent.Query))
        {
            int divValue = Convert.ToInt32(spinDivValue.Number);
            
            GC.Collect();

            DateTime timeStart = DateTime.Now;

            InfoGrid.DataSource = GetXPCollectionSource(divValue);
            InfoGrid.DataBind();

            DateTime timeEnd = DateTime.Now;

            long numMemory = GC.GetTotalMemory(true);

            TimeSpan t1 = timeEnd - timeStart;

            double dm = numMemory / 1024d;

            string msg = string.Format("分配内存[{1}]K字节,用时[{0}]毫秒", t1.TotalMilliseconds, dm.ToString("F2"));


            Label lbl = InfoGrid.FindStatusBarTemplateControl("lblStatusBar") as Label;
            lbl.Text = msg;
        }
    }
    #endregion

    #region CusorGrid_PreRender

    protected void CusorGrid_PreRender(object sender, EventArgs e)
    {
        // 对列表的每一列进行自动的设置，包括
        // 1、根据字段长度自动调整列宽
        // 2、设置编辑页面的文本不换行
        AutoSettingGridColumnStyle(CusorGrid);
    }

    #endregion

    #region CusorGrid_CustomCallback

    protected void CusorGrid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        // 统一处理新增，批量删除，改变分页风格，打开/关闭过滤行事件
        if (HandleDefaultEvent(e.Parameters, CusorGrid, new Product_BLL(XpoSession)))
            return;

        // 不能统一处理的事件，由页面自行处理

        /**
         * 这里没有使用ObjectDataSource，而是直接绑定，
         * 是为了在状态行中准确提示查询耗时与内存用量
         * 不应该在其他页面中使用这种调用方法
         */
        if (e.Parameters.Equals(GEvent.Query))
        {
            int divValue = Convert.ToInt32(spinDivValue.Number);
            
            GC.Collect();

            DateTime timeStart = DateTime.Now;

            dataSource.Criteria = "[DivValue] <= ?";
            dataSource.CriteriaParameters.Add("DivValue", TypeCode.Int32, Convert.ToInt32(spinDivValue.Number).ToString());
            CusorGrid.DataBind();

            DateTime timeEnd = DateTime.Now;

            long numMemory = GC.GetTotalMemory(true);

            TimeSpan t1 = timeEnd - timeStart;

            double dm = numMemory / 1024d;

            string msg = string.Format("分配内存[{1}]K字节,用时[{0}]毫秒", t1.TotalMilliseconds, dm.ToString("F2"));


            Label lbl = CusorGrid.FindStatusBarTemplateControl("lblStatusBar") as Label;
            lbl.Text = msg;
        }
    }
    #endregion

    /***
     *  DataSource
     */

    #region GetXPCollectionSource

    private XPCollection<Product> GetXPCollectionSource(int divValue)
    {
        return new XPCollection<Product>(XpoSession,
           new BinaryOperator("DivValue", divValue, BinaryOperatorType.LessOrEqual) );
    }

    #endregion

}
